package com.xbao.dao;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.xbao.model.PeriodRule;
import com.xbao.model.PeriodRuleExample.Criteria;
import com.xbao.model.PeriodRuleExample.Criterion;
import com.xbao.model.PeriodRuleExample;
import java.util.List;
import java.util.Map;

public class PeriodRuleSqlProvider {

    public String countByExample(PeriodRuleExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("period_rule");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(PeriodRuleExample example) {
        BEGIN();
        DELETE_FROM("period_rule");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(PeriodRule record) {
        BEGIN();
        INSERT_INTO("period_rule");
        
        if (record.getName() != null) {
            VALUES("name", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getStarttime() != null) {
            VALUES("starttime", "#{starttime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            VALUES("endtime", "#{endtime,jdbcType=BIGINT}");
        }
        
        if (record.getClasstime() != null) {
            VALUES("classtime", "#{classtime,jdbcType=VARCHAR}");
        }
        
        if (record.getEid() != null) {
            VALUES("eid", "#{eid,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            VALUES("subid", "#{subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotaltimes() != null) {
            VALUES("totaltimes", "#{totaltimes,jdbcType=BIGINT}");
        }
        
        if (record.getRuletype() != null) {
            VALUES("ruletype", "#{ruletype,jdbcType=INTEGER}");
        }
        
        if (record.getCid() != null) {
            VALUES("cid", "#{cid,jdbcType=BIGINT}");
        }
        
        if (record.getOrgid() != null) {
            VALUES("orgid", "#{orgid,jdbcType=BIGINT}");
        }
        
        if (record.getComments() != null) {
            VALUES("comments", "#{comments,jdbcType=VARCHAR}");
        }
        
        if (record.getFeetype() != null) {
            VALUES("feetype", "#{feetype,jdbcType=INTEGER}");
        }
        
        if (record.getAlert() != null) {
            VALUES("alert", "#{alert,jdbcType=INTEGER}");
        }
        
        return SQL();
    }

    public String selectByExample(PeriodRuleExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("name");
        SELECT("starttime");
        SELECT("endtime");
        SELECT("classtime");
        SELECT("eid");
        SELECT("subid");
        SELECT("totaltimes");
        SELECT("ruletype");
        SELECT("cid");
        SELECT("orgid");
        SELECT("comments");
        SELECT("feetype");
        SELECT("alert");
        FROM("period_rule");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        PeriodRule record = (PeriodRule) parameter.get("record");
        PeriodRuleExample example = (PeriodRuleExample) parameter.get("example");
        
        BEGIN();
        UPDATE("period_rule");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getName() != null) {
            SET("name = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getStarttime() != null) {
            SET("starttime = #{record.starttime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            SET("endtime = #{record.endtime,jdbcType=BIGINT}");
        }
        
        if (record.getClasstime() != null) {
            SET("classtime = #{record.classtime,jdbcType=VARCHAR}");
        }
        
        if (record.getEid() != null) {
            SET("eid = #{record.eid,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            SET("subid = #{record.subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotaltimes() != null) {
            SET("totaltimes = #{record.totaltimes,jdbcType=BIGINT}");
        }
        
        if (record.getRuletype() != null) {
            SET("ruletype = #{record.ruletype,jdbcType=INTEGER}");
        }
        
        if (record.getCid() != null) {
            SET("cid = #{record.cid,jdbcType=BIGINT}");
        }
        
        if (record.getOrgid() != null) {
            SET("orgid = #{record.orgid,jdbcType=BIGINT}");
        }
        
        if (record.getComments() != null) {
            SET("comments = #{record.comments,jdbcType=VARCHAR}");
        }
        
        if (record.getFeetype() != null) {
            SET("feetype = #{record.feetype,jdbcType=INTEGER}");
        }
        
        if (record.getAlert() != null) {
            SET("alert = #{record.alert,jdbcType=INTEGER}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("period_rule");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("name = #{record.name,jdbcType=VARCHAR}");
        SET("starttime = #{record.starttime,jdbcType=BIGINT}");
        SET("endtime = #{record.endtime,jdbcType=BIGINT}");
        SET("classtime = #{record.classtime,jdbcType=VARCHAR}");
        SET("eid = #{record.eid,jdbcType=BIGINT}");
        SET("subid = #{record.subid,jdbcType=BIGINT}");
        SET("totaltimes = #{record.totaltimes,jdbcType=BIGINT}");
        SET("ruletype = #{record.ruletype,jdbcType=INTEGER}");
        SET("cid = #{record.cid,jdbcType=BIGINT}");
        SET("orgid = #{record.orgid,jdbcType=BIGINT}");
        SET("comments = #{record.comments,jdbcType=VARCHAR}");
        SET("feetype = #{record.feetype,jdbcType=INTEGER}");
        SET("alert = #{record.alert,jdbcType=INTEGER}");
        
        PeriodRuleExample example = (PeriodRuleExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(PeriodRule record) {
        BEGIN();
        UPDATE("period_rule");
        
        if (record.getName() != null) {
            SET("name = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getStarttime() != null) {
            SET("starttime = #{starttime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            SET("endtime = #{endtime,jdbcType=BIGINT}");
        }
        
        if (record.getClasstime() != null) {
            SET("classtime = #{classtime,jdbcType=VARCHAR}");
        }
        
        if (record.getEid() != null) {
            SET("eid = #{eid,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            SET("subid = #{subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotaltimes() != null) {
            SET("totaltimes = #{totaltimes,jdbcType=BIGINT}");
        }
        
        if (record.getRuletype() != null) {
            SET("ruletype = #{ruletype,jdbcType=INTEGER}");
        }
        
        if (record.getCid() != null) {
            SET("cid = #{cid,jdbcType=BIGINT}");
        }
        
        if (record.getOrgid() != null) {
            SET("orgid = #{orgid,jdbcType=BIGINT}");
        }
        
        if (record.getComments() != null) {
            SET("comments = #{comments,jdbcType=VARCHAR}");
        }
        
        if (record.getFeetype() != null) {
            SET("feetype = #{feetype,jdbcType=INTEGER}");
        }
        
        if (record.getAlert() != null) {
            SET("alert = #{alert,jdbcType=INTEGER}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(PeriodRuleExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}